import { ENTERPRISE_STATUS, ENTERPRISE_ACCOUNTAUTHENTICATION } from "utils/Enum"
import { getAuthenticationStatus } from 'services/EnterpriseCertificationListBackUpsServoce'
import { Toast } from 'antd-mobile';
export default {
  namespaces: 'EnterpriseCertificationListBackUpsModel',
  state: {
    options: [{
      id: 'RealNameCertification',
      title: '申请人实名认证',
      status: 'padding'
    }, {
      id: 'EnterpriseRealNameCertification',
      title: '填写企业信息',
      msg: '需营业执照',
      status: 'padding' //padding success error
    }]
  },
  reducers: {
    setStateOptions(state, { payload }) {
      let { identityStatus, status } = payload
      identityStatus = 'P' + identityStatus

      let options = [...state.options]
      let states;
      switch (identityStatus) {
        case ENTERPRISE_STATUS.PINCOMPLETE:
          states = 'padding'
          break
        case ENTERPRISE_STATUS.PPASSED:
          states = 'success'
          break
        case ENTERPRISE_STATUS.PDENY:
          states = 'error'
          break
        default:
          states = 'padding'
          break
      }
      options[0].status = states
      switch (status) {
        case ENTERPRISE_ACCOUNTAUTHENTICATION.WAITING:
          states = 'padding'
          break
        case ENTERPRISE_ACCOUNTAUTHENTICATION.PRIMARY_PASSED:
          states = 'success'
          break
        case ENTERPRISE_ACCOUNTAUTHENTICATION.PRIMARY_DENY:
          states = 'error'
          break
        default:
          states = 'padding'
          break
      }
      options[1].status = states
      return {
        ...state,
        options
      }
    }
  },
  effects: {
    *getStatus({ payload, callback }, { call, put }) {
      let res = yield call(getAuthenticationStatus, payload)
      let { code } = res
      let reg = new RegExp(/0000$/, 'i')
      if (reg.test(code)) {
        let {
          enterpriseAccount: { identityStatus },
          enterpriseAccountAuthentication: { status }
        } = res
        yield put({
          type: 'setStateOptions',
          payload: {
            identityStatus,  //个人
            status  //企业
          }
        })
        yield callback('success')
      } else {
        yield callback('error')
      }

    }
  }
}
